
var app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    normal: '/images/normal.png',
    normal1: '/images/normal.png',
    normal2: '/images/normal.png',
    normal3: '/images/normal.png',
    normal4: '/images/normal.png',
    normal5: '/images/normal.png',
    selected: '/images/selected.png',
    dicId:0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var types = options.types;
    var id = options.id;
    var sid = options.sid;
    var url = options.url;
    var name = options.name;
    var shop = options.shop;
    var surl = options.surl;
    var userInfoAvatar = '';
    var nickName = '';
    var openId = app.globalData.openId;
    var that = this
    var names = '';
    var urls = ''
    if (types == 2){
      names = shop;
      urls = surl;
      that.setData({
        names: names,
        urls: urls
      })
    }else if(types == 3){
      names = name;
      urls = url;
      that.setData({
        names: names,
        urls: urls
      })
    }
    //调用应用实例的方法获取全局数据
    app.getUserInfo(function (userInfo) {
      //更新数据
      console.log(userInfo)
      that.setData({
        userInfoAvatar: userInfo.avatarUrl,
        nickName: userInfo.nickName,
        types: types,
        id: id,
        openId: openId,
        sid: sid,
      })
    })
  },

  onContent:function(e){
    var content = e.detail.value;
    this.setData({
      content:content
    })
  },

  star1: function (e) {
    var dicId = this.data.dicId;
    var normal = this.data.normal;
    var selected = this.data.selected;
    dicId = 1;
    this.setData({
      normal1: selected,
      normal2: normal,
      normal3: normal,
      normal4: normal,
      normal5: normal,
      dicId: dicId
    })
    console.log('点击第一个星')
    
  },

  star2: function (e) {
    var dicId = this.data.dicId;
    var normal = this.data.normal;
    var selected = this.data.selected;
    dicId = 2;
    this.setData({
      normal1: selected,
      normal2: selected,
      normal3: normal,
      normal4: normal,
      normal5: normal,
      dicId: dicId
    })
    console.log('点击第2个星')
    
  },

  star3: function (e) {
    var dicId = this.data.dicId;
    var normal = this.data.normal;
    var selected = this.data.selected;
    dicId = 3;
    this.setData({
      normal1: selected,
      normal2: selected,
      normal3: selected,
      normal4: normal,
      normal5: normal,
      dicId: dicId
    })
    console.log('点击第3个星')
    
  },

  star4: function (e) {
    var dicId = this.data.dicId;
    var normal = this.data.normal;
    var selected = this.data.selected;
    dicId = 4;
    this.setData({
      normal1: selected,
      normal2: selected,
      normal3: selected,
      normal4: selected,
      normal5: normal,
      dicId: dicId
    })
    console.log('点击第4个星')
    
  },

  star5: function (e) {
    var dicId = this.data.dicId;
    var selected = this.data.selected;
    dicId = 5;
    this.setData({
      normal1: selected,
      normal2: selected,
      normal3: selected,
      normal4: selected,
      normal5: selected,
      dicId: dicId
    })
    console.log('点击第5个星')
    
  },

  onSubmit:function(e){
    wx.showLoading({
      title: '提交中..',
    })
    // 参数  openId （用户id）、
    // proId（外卖是商铺id，创客是商品id）
    // nickName（用户名） 
    // userInfoAvatar（用户头像） 
    // type（订单分类） 
    // orderId（订单id）
    // appContent（评价内容）
    // dicId（星级 1、2、3、4、5 代替）
    var that = this;
    var userInfoAvatar = that.data.userInfoAvatar;
    var nickName = that.data.nickName;
    var types = that.data.types;
    var id = that.data.id;
    var openId = that.data.openId;
    var dicId = that.data.dicId;
    var content = that.data.content;
    var sid = that.data.sid;

    if(dicId == 0 || content == ''){
      wx.showToast({
        title: '请完善评价信息',
      })
    }else{

      if(types == 2){
        wx.request({
          url: app.globalData.url+ 'addAppraise?openId=' + openId + '&userInfoAvatar='
          + userInfoAvatar + '&nickName=' + nickName + '&type=' + types + '&orderId=' + id
          + '&appContent=' + content + '&dicId=' + dicId +'&shopId='+sid,
          success: function (res) {
            if (res.data == "ajaxSuccess"){
              wx.hideLoading();
              wx.showToast({
                title: '评价成功',
              })
              setTimeout(function(){
                wx.redirectTo({
                  url: '/pages/mine/allorder/allorder',
                })
              },1000)
              
            }
          }
        })
      }else if(types == 3){
        wx.request({
          url: app.globalData.url+ 'addAppraise?openId=' + openId + '&userInfoAvatar='
          + userInfoAvatar + '&nickName=' + nickName + '&type=' + types + '&orderId=' + id
          + '&appContent=' + content + '&dicId=' + dicId,
          success: function (res) {
            if (res.data == "ajaxSuccess") {
              wx.hideLoading();
              wx.showToast({
                title: '评价成功',
              })
              setTimeout(function () {
                wx.redirectTo({
                  url: '/pages/mine/allorder/allorder',
                })
              }, 1000)

            }
          }
        })
      }
      
    }
  }

})